{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Examples require an initialized GsSession and relevant entitlements. External clients need to substitute thier own client id and client secret below. Please refer to [Authentication](https://developer.gs.com/p/docs/institutional/platform/authentication/) for details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('read_product_data',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How to query data \n",
    "The Data APIs support many ways to query datasets to intuitively fetch only the data users need.\n",
    "More details on [Querying Data](https://developer.gs.com/p/docs/services/data/data-access/query-data/) can be found in the documentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date, timedelta, datetime\n",
    "from gs_quant.data import Dataset\n",
    "import pydash"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Data in Marquee is available in the form of Datasets (collections of homogenous data). Each Dataset has a set of entitlements, a fixed schema, and assets in coverage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_id = 'FXIVOL_STANDARD' # https://marquee.gs.com/s/developer/datasets/FXIVOL_STANDARD\n",
    "ds = Dataset(dataset_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Data for limited number of assets or spanning a small time frame can be queried in one go by specifying the assets to query and date/time range."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>assetId</th>\n",
       "      <th>bbid</th>\n",
       "      <th>tenor</th>\n",
       "      <th>strikeReference</th>\n",
       "      <th>relativeStrike</th>\n",
       "      <th>impliedVolatility</th>\n",
       "      <th>updateTime</th>\n",
       "      <th>absoluteStrike</th>\n",
       "      <th>expirationDate</th>\n",
       "      <th>settlementDate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-15</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>delta</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.070937</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.516844</td>\n",
       "      <td>2019-02-15</td>\n",
       "      <td>2019-02-19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-16</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>delta</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.068017</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.512725</td>\n",
       "      <td>2019-02-15</td>\n",
       "      <td>2019-02-19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-17</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>delta</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.065493</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.513737</td>\n",
       "      <td>2019-02-20</td>\n",
       "      <td>2019-02-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-18</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>delta</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.064647</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.511394</td>\n",
       "      <td>2019-02-20</td>\n",
       "      <td>2019-02-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-15</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>forward</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.070936</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.517168</td>\n",
       "      <td>2019-02-15</td>\n",
       "      <td>2019-02-19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     assetId    bbid tenor strikeReference  relativeStrike  \\\n",
       "date                                                                         \n",
       "2019-01-15  MA0DRZ2E7EHKTZ82  EURCAD    1m           delta             0.0   \n",
       "2019-01-16  MA0DRZ2E7EHKTZ82  EURCAD    1m           delta             0.0   \n",
       "2019-01-17  MA0DRZ2E7EHKTZ82  EURCAD    1m           delta             0.0   \n",
       "2019-01-18  MA0DRZ2E7EHKTZ82  EURCAD    1m           delta             0.0   \n",
       "2019-01-15  MA0DRZ2E7EHKTZ82  EURCAD    1m         forward           100.0   \n",
       "\n",
       "            impliedVolatility          updateTime  absoluteStrike  \\\n",
       "date                                                                \n",
       "2019-01-15           0.070937 2020-10-28 23:38:03        1.516844   \n",
       "2019-01-16           0.068017 2020-10-28 23:38:03        1.512725   \n",
       "2019-01-17           0.065493 2020-10-28 23:38:03        1.513737   \n",
       "2019-01-18           0.064647 2020-10-28 23:38:03        1.511394   \n",
       "2019-01-15           0.070936 2020-10-28 23:38:03        1.517168   \n",
       "\n",
       "           expirationDate settlementDate  \n",
       "date                                      \n",
       "2019-01-15     2019-02-15     2019-02-19  \n",
       "2019-01-16     2019-02-15     2019-02-19  \n",
       "2019-01-17     2019-02-20     2019-02-22  \n",
       "2019-01-18     2019-02-20     2019-02-22  \n",
       "2019-01-15     2019-02-15     2019-02-19  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start_date = date(2019, 1, 15)\n",
    "end_date = date(2019, 1, 18)\n",
    "\n",
    "data = ds.get_data(start_date, end_date, bbid=['EURCAD'])\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instead of a range, one can also specify a set of date/times to get data for just those specific date/times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>assetId</th>\n",
       "      <th>bbid</th>\n",
       "      <th>tenor</th>\n",
       "      <th>strikeReference</th>\n",
       "      <th>relativeStrike</th>\n",
       "      <th>impliedVolatility</th>\n",
       "      <th>updateTime</th>\n",
       "      <th>absoluteStrike</th>\n",
       "      <th>expirationDate</th>\n",
       "      <th>settlementDate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-15</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>delta</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.070937</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.516844</td>\n",
       "      <td>2019-02-15</td>\n",
       "      <td>2019-02-19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-18</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>delta</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.064647</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.511394</td>\n",
       "      <td>2019-02-20</td>\n",
       "      <td>2019-02-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-15</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>forward</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.070936</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.517168</td>\n",
       "      <td>2019-02-15</td>\n",
       "      <td>2019-02-19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-18</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>forward</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.064650</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.511679</td>\n",
       "      <td>2019-02-20</td>\n",
       "      <td>2019-02-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-15</th>\n",
       "      <td>MA0DRZ2E7EHKTZ82</td>\n",
       "      <td>EURCAD</td>\n",
       "      <td>1m</td>\n",
       "      <td>spot</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.070965</td>\n",
       "      <td>2020-10-28 23:38:03</td>\n",
       "      <td>1.514112</td>\n",
       "      <td>2019-02-15</td>\n",
       "      <td>2019-02-19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     assetId    bbid tenor strikeReference  relativeStrike  \\\n",
       "date                                                                         \n",
       "2019-01-15  MA0DRZ2E7EHKTZ82  EURCAD    1m           delta             0.0   \n",
       "2019-01-18  MA0DRZ2E7EHKTZ82  EURCAD    1m           delta             0.0   \n",
       "2019-01-15  MA0DRZ2E7EHKTZ82  EURCAD    1m         forward           100.0   \n",
       "2019-01-18  MA0DRZ2E7EHKTZ82  EURCAD    1m         forward           100.0   \n",
       "2019-01-15  MA0DRZ2E7EHKTZ82  EURCAD    1m            spot           100.0   \n",
       "\n",
       "            impliedVolatility          updateTime  absoluteStrike  \\\n",
       "date                                                                \n",
       "2019-01-15           0.070937 2020-10-28 23:38:03        1.516844   \n",
       "2019-01-18           0.064647 2020-10-28 23:38:03        1.511394   \n",
       "2019-01-15           0.070936 2020-10-28 23:38:03        1.517168   \n",
       "2019-01-18           0.064650 2020-10-28 23:38:03        1.511679   \n",
       "2019-01-15           0.070965 2020-10-28 23:38:03        1.514112   \n",
       "\n",
       "           expirationDate settlementDate  \n",
       "date                                      \n",
       "2019-01-15     2019-02-15     2019-02-19  \n",
       "2019-01-18     2019-02-20     2019-02-22  \n",
       "2019-01-15     2019-02-15     2019-02-19  \n",
       "2019-01-18     2019-02-20     2019-02-22  \n",
       "2019-01-15     2019-02-15     2019-02-19  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = ds.get_data(dates=[date(2019, 1, 15), date(2019, 1, 18)],\n",
    "                   bbid=['EURCAD'])\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For larger number of assets or for longer time ranges, \n",
    "we recommend iterating over assets and time to avoid hitting API query limits. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# loop over assets\n",
    "def iterate_over_assets(dataset, coverage, start, end, batch_size=5, query_dimension='assetId', delta=timedelta(days=6)):\n",
    "    for ids in pydash.chunk(coverage[query_dimension].tolist(), size=batch_size):\n",
    "        print('iterate over assets', ids)\n",
    "        iterate_over_time(start, end, ids, dataset, delta=delta, query_dimension=query_dimension)\n",
    "\n",
    "# loop over time\n",
    "def iterate_over_time(start, end, ids, dataset, delta=timedelta(days=6), query_dimension='assetId'):\n",
    "    iter_start = start\n",
    "    while iter_start < end:\n",
    "        iter_end = min(iter_start + delta, end)\n",
    "        print('time iteration since', iter_start, 'until', iter_end)\n",
    "        data = dataset.get_data(iter_start, iter_end, **{query_dimension: ids})\n",
    "        # Add your code here to make use of fetched data\n",
    "        \n",
    "        iter_start = iter_end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iterate over assets ['MA4B66MW5E27U8NN8LS', 'MA4B66MW5E27U8NN8N6', 'MA4B66MW5E27U8NN8SP', 'MA4B66MW5E27U8NN922', 'MA4B66MW5E27U8NN95P']\n",
      "time iteration since 2021-05-01 until 2021-05-07\n",
      "time iteration since 2021-05-07 until 2021-05-13\n",
      "time iteration since 2021-05-13 until 2021-05-19\n",
      "time iteration since 2021-05-19 until 2021-05-25\n",
      "time iteration since 2021-05-25 until 2021-05-31\n",
      "iterate over assets ['MA4B66MW5E27U8NPZQY', 'MA4B66MW5E27U8P3294', 'MA4B66MW5E27U8P3295', 'MA4B66MW5E27U8P32DM', 'MA4B66MW5E27U8P32GB']\n",
      "time iteration since 2021-05-01 until 2021-05-07\n",
      "time iteration since 2021-05-07 until 2021-05-13\n",
      "time iteration since 2021-05-13 until 2021-05-19\n",
      "time iteration since 2021-05-19 until 2021-05-25\n",
      "time iteration since 2021-05-25 until 2021-05-31\n",
      "iterate over assets ['MA4B66MW5E27U8P32KK', 'MA4B66MW5E27U8P32LE', 'MA4B66MW5E27U8P32LH', 'MA4B66MW5E27U8P32LQ', 'MA4B66MW5E27U8P32PC']\n",
      "time iteration since 2021-05-01 until 2021-05-07\n",
      "time iteration since 2021-05-07 until 2021-05-13\n",
      "time iteration since 2021-05-13 until 2021-05-19\n",
      "time iteration since 2021-05-19 until 2021-05-25\n",
      "time iteration since 2021-05-25 until 2021-05-31\n",
      "iterate over assets ['MA4B66MW5E27U8P32S9', 'MA4B66MW5E27U8P32SB', 'MA4B66MW5E27U8P32SR', 'MA4B66MW5E27U8P32SY', 'MA4B66MW5E27U8P32XG']\n",
      "time iteration since 2021-05-01 until 2021-05-07\n",
      "time iteration since 2021-05-07 until 2021-05-13\n",
      "time iteration since 2021-05-13 until 2021-05-19\n",
      "time iteration since 2021-05-19 until 2021-05-25\n",
      "time iteration since 2021-05-25 until 2021-05-31\n",
      "iterate over assets ['MA4B66MW5E27UAEP4R5', 'MA4B66MW5E27UAEP4RT', 'MA4B66MW5E27UAGYYRR', 'MA4B66MW5E27UANZH2R', 'MA4B66MW5E27UANZH2T']\n",
      "time iteration since 2021-05-01 until 2021-05-07\n",
      "time iteration since 2021-05-07 until 2021-05-13\n",
      "time iteration since 2021-05-13 until 2021-05-19\n",
      "time iteration since 2021-05-19 until 2021-05-25\n",
      "time iteration since 2021-05-25 until 2021-05-31\n"
     ]
    }
   ],
   "source": [
    "dataset_id = 'EDRVOL_PERCENT_V1_STANDARD'  # https://marquee.gs.com/s/developer/datasets/EDRVOL_PERCENT_V1_STANDARD \n",
    "ds = Dataset(dataset_id)\n",
    "\n",
    "coverage = ds.get_coverage()\n",
    "\n",
    "iterate_over_assets(ds, coverage, date(2021, 5, 1), date(2021, 5, 31), batch_size=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similar approach can be used to download all data of a dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "coverage = ds.get_coverage(include_history=True)\n",
    "coverage = coverage.sort_values(by='historyStartDate', axis=0)\n",
    "start_date = datetime.strptime(coverage['historyStartDate'].values[0], '%Y-%m-%d').date()\n",
    "\n",
    "# warning: long running operation\n",
    "iterate_over_assets(ds, coverage, start_date, date.today())\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
